<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册 - 语义网络</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'PingFang SC', 'Microsoft YaHei', sans-serif;
            background: linear-gradient(135deg, #e6f0ff 0%, #f8e8e8 50%, #fff3e0 100%);
            height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            color: #2c3e50;
        }

        .register-container {
            background: rgba(255, 255, 255, 0.95);
            padding: 2.5rem;
            border-radius: 15px;
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
            width: 100%;
            max-width: 500px;
            backdrop-filter: blur(10px);
        }

        .register-header {
            text-align: center;
            margin-bottom: 2.5rem;
        }

        .register-header h1 {
            color: #1a2a6c;
            font-size: 2.2rem;
            margin-bottom: 0.8rem;
            font-weight: 600;
        }

        .register-header p {
            color: #7f8c8d;
            font-size: 1rem;
            line-height: 1.5;
        }

        .form-group {
            margin-bottom: 1.8rem;
            position: relative;
        }

        .form-group label {
            display: block;
            margin-bottom: 0.6rem;
            color: #2c3e50;
            font-weight: 500;
            font-size: 1.1rem;
        }

        .form-group input {
            width: 100%;
            padding: 1rem;
            border: 2px solid #e0e0e0;
            border-radius: 8px;
            font-size: 1rem;
            transition: all 0.3s ease;
            background: rgba(255, 255, 255, 0.9);
        }

        .form-group input:focus {
            outline: none;
            border-color: #1a2a6c;
            box-shadow: 0 0 0 3px rgba(26, 42, 108, 0.1);
        }

        .register-btn {
            width: 100%;
            padding: 1.2rem;
            background: linear-gradient(135deg, #1a2a6c 0%, #b21f1f 100%);
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 1.1rem;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.3s ease;
            margin-top: 1rem;
        }

        .register-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
        }

        .links-container {
            display: flex;
            justify-content: center;
            margin-top: 1.5rem;
        }

        .link {
            color: #1a2a6c;
            text-decoration: none;
            font-size: 0.9rem;
            transition: color 0.3s ease;
        }

        .link:hover {
            color: #b21f1f;
            text-decoration: underline;
        }

        .footer {
            text-align: center;
            margin-top: 2rem;
            color: #7f8c8d;
            font-size: 0.9rem;
            padding-top: 1rem;
            border-top: 1px solid #eee;
        }

        .semantic-icon {
            font-size: 2.5rem;
            margin-bottom: 1rem;
            color: #1a2a6c;
        }

        .input-icon {
            position: absolute;
            right: 1rem;
            top: 2.8rem;
            color: #7f8c8d;
        }

        .password-requirements {
            font-size: 0.8rem;
            color: #7f8c8d;
            margin-top: 0.5rem;
        }

        @media (max-width: 480px) {
            .register-container {
                margin: 1rem;
                padding: 2rem;
            }
        }
    </style>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
</head>

<body>
    <div class="register-container">
        <div class="register-header">
            <i class="fas fa-user-plus semantic-icon"></i>
            <h1>创建新账户</h1>
            <p>加入语义网络，开启数据互联之旅</p>
        </div>
        <form id="registerForm">
            <div class="form-group">
                <label for="username">用户名</label>
                <input type="text" id="username" name="username" required placeholder="请输入用户名">
                <i class="fas fa-user input-icon"></i>
            </div>
            <div class="form-group">
                <label for="password">密码</label>
                <input type="password" id="password" name="password" required placeholder="请输入密码">
                <i class="fas fa-lock input-icon"></i>
                <div class="password-requirements">
                    密码至少包含8个字符，包括字母和数字
                </div>
            </div>
            <div class="form-group">
                <label for="confirm-password">确认密码</label>
                <input type="password" id="confirm-password" name="confirm-password" required placeholder="请再次输入密码">
                <i class="fas fa-lock input-icon"></i>
            </div>
            <button type="submit" class="register-btn">注册</button>
            <div class="links-container">
                <a href="login.html" class="link">已有账户？立即登录</a>
            </div>
        </form>
        <div class="footer">
            <p>© 2024 语义网络平台 - 连接数据，创造价值</p>
        </div>
    </div>
    <script src="common.js"></script>

    <script>
        document.getElementById('registerForm').addEventListener('submit', async function (e) {
            e.preventDefault();

            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            const confirmPassword = document.getElementById('confirm-password').value;

            if (password !== confirmPassword) {
                alert('两次输入的密码不一致');
                return;
            }
            try {
                const body = `username=${encodeURIComponent(username)}&password=${encodeURIComponent(password)}`;
                const result = await post('/manage/api/auth/register', body);
                alert('注册成功，请登录');
                window.location.href = 'login.html';
            }
            catch (error) {
                alert(error.message);
            }
        });

        async function post(path, body) {
            try {
                const response = await fetch(path, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded',
                    },
                    body: body
                })
                const data = await response.json();
                if (!data.success) {
                    throw new Error(data.errorMessage);
                }
                return data.data;
            } catch (error) {
                console.error('error:', error);
                throw error;
            }
        }

    </script>
</body>

</html>